<?php

/**
 * goods actions.
 *
 * @package    WacStorehouse
 * @subpackage goods
 * @author     JianBinBi
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class goodsActions extends WacCommonActions
{

  /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as select html format
   */
    public function executeGetCottonHashInFormat(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();

//        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("code", WacGoodsCategory::$predefinedClassCodes["classCottonCode"]);
//
//        $ids = array($objGoodsCategory1->getId());
//        $items = $this->mainModuleTable->getIdNameHashListByGoodsCategorys($ids, 500, "t1.name asc");

        $items = $this->getHashInFormatByGoodsCategory(WacGoodsCategory::$predefinedClassCodes["classCottonCode"]);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }


  /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as select html format
   */
    public function executeGetDyeHashInFormat(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();

        $items = $this->getHashInFormatByGoodsCategory(WacGoodsCategory::$predefinedClassCodes["classDyeCode"]);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

   /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as select html format
   */
    public function executeGetWeaveHashInFormat(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();

        $items = $this->getHashInFormatByGoodsCategory(WacGoodsCategory::$predefinedClassCodes["classWeaveCode"]);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as select html format
   */
    public function executeGetProductHashInFormat(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();
        $items = $this->getHashInFormatByGoodsCategory(
                array(
                    WacGoodsCategory::$predefinedClassCodes["classWeaveCode"],
                    WacGoodsCategory::$predefinedClassCodes["classCleanUpCode"],
                    WacGoodsCategory::$predefinedClassCodes["classFinalClothCode"],
            ));
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as select html format
   */
    public function executeGetCleanUpHashInFormat(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();

        $items = $this->getHashInFormatByGoodsCategory(WacGoodsCategory::$predefinedClassCodes["classCleanUpCode"]);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as select html format
   */
    public function executeGetFinalClothHashInFormat(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();

        $items = $this->getHashInFormatByGoodsCategory(WacGoodsCategory::$predefinedClassCodes["classFinalClothCode"]);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }
  

    /*
     * $goodsCategoryCodes - array() or string
   *  @return id=>name hash as select html format
   */
    protected function getHashInFormatByGoodsCategory($goodsCategoryCodes) {

        $resultSet = array();

        if(!is_array($goodsCategoryCodes))
        {
            $objGoodsCategory = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("code", $goodsCategoryCodes);
            $ids = array($objGoodsCategory->getId());
        }
        else
        {
            $ids = Doctrine::getTable(WacTable::$goodsCategory)->getIdsByCodes($goodsCategoryCodes);
        }
        
        $items = $this->mainModuleTable->getIdNameHashListByGoodsCategorys($ids, 500, "t1.name asc");
        return $items;
    }

    /*
    * override filter list
    */
    public function filterList($listObjs) {
        $filterArr = array();
        if(count($listObjs) > 0) {
            foreach($listObjs as $listObj) {
                $tmpArr = $listObj->toArray();
                $tmpArr['goods_category_id']   = $listObj->getWacGoodsCategory()->getName();
                $tmpArr['jar_id']              = $listObj->getWacJar()->getName();
                $tmpArr['color_id']            = $listObj->getWacColor()->getName();
                $tmpArr['axis_id']             = $listObj->getWacAxis()->getName();

                $filterArr[] = $tmpArr;
            }
        }

        return $filterArr;
    }
}
